package com.wyl.niupi.yxc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wyl.niupi.framework.constant.Const;
import com.wyl.niupi.framework.util._BeanUtil;
import com.wyl.niupi.framework.vo.PageResultVo;
import com.wyl.niupi.framework.vo.ResponseVo;
import com.wyl.niupi.framework.vo.Vo;
import com.wyl.niupi.frameworkExt.controller.BaseControllerExt;
import com.wyl.niupi.yxc.entity.YxcGoods;
import com.wyl.niupi.yxc.service.YxcGoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @author wyl
 * @since 2020-06-04
 */
@Tag(name = "YxcGoodsController", description = "YxcGoods相关接口")
@RestController
@RequestMapping("/yxc/yxcGoods")
public class YxcGoodsController extends BaseControllerExt {

    @Autowired
    private YxcGoodsService yxcGoodsService;

    @Operation(summary = "获取商品列表", parameters = {
        @Parameter(name = "where_字段名称_过滤规则", example = "where_cname_like", required = false),
        @Parameter(name = "page", example = "1", description = "如果page和row都不为空则进行分页", required = false),
        @Parameter(name = "rows", example = "15", description = "如果page和row都不为空则进行分页", required = false),
    }, description = "详细描述...")
    @GetMapping("/list")
    public ResponseVo<PageResultVo<List<YxcGoods>>> list(HttpServletRequest request){
        QueryWrapper queryWrapper = getQueryWrapper(request);
        queryWrapper.eq("is_delete", Const.IS_NO);

        IPage<YxcGoods> ipage = getPage(request);
        if (null != ipage) {
            ipage = yxcGoodsService.selectPage(ipage, queryWrapper);
            List<Vo<YxcGoods>> list = Vo.list(ipage.getRecords());
            list.forEach(item -> {
                item.put("sellPrice", "***");
            });
            return sendSuccess(new PageResultVo(list, ipage.getTotal()));
        } else {
            List list = yxcGoodsService.list(queryWrapper);
            return sendSuccess(new PageResultVo(list, list.size()));
        }
    }

    @Operation(summary = "根据id获取商品", parameters = {
        @Parameter(name = "lid", example = "1", description = "主键", required = true),
    })
    @GetMapping("/get")
    public ResponseVo<YxcGoods> get(@RequestParam("lid") Integer lid) {
        YxcGoods bo = yxcGoodsService.getById(lid);
        return sendSuccess(bo);
    }

    @Operation(summary = "添加商品")
    @PostMapping("/add")
    public ResponseVo add(@Parameter(name = "yxcGoods", description = "商品") YxcGoods bo) {
        bo.setIsDelete(Const.IS_NO);
        yxcGoodsService.save(bo);
        return sendSuccess();
    }

    @Operation(summary = "根据id删除商品", parameters = {
        @Parameter(name = "lid", example = "1", description = "主键", required = true),
    })
    @PostMapping("/delete")
    public ResponseVo delete(@RequestParam("lid") Integer lid) {
        yxcGoodsService.removeById(lid);
        return sendSuccess();
    }

    @Operation(summary = "根据id修改商品", parameters = {
        @Parameter(name = "lid", example = "1", description = "主键", required = false),
        @Parameter(name = "其他商品属性", description = "传什么属性就修改什么，不传的属性不修改", required = false),
    })
    @PostMapping("/update")
    public ResponseVo update(@RequestParam("lid") Integer lid, HttpServletRequest request) {
        try {
            YxcGoods oldBo = yxcGoodsService.getById(lid);
            _BeanUtil.copyPropertiesFromRequest(oldBo, request);
            yxcGoodsService.saveOrUpdate(oldBo);
            return sendSuccess();
        }catch (IllegalAccessException | InvocationTargetException e) {
            return sendError(e.getMessage());
        }
    }


}
